package cn.ronghuanet.auth.controller;

import cn.ronghuanet.auth.dto.RoleDTO.RoleDTO;
import cn.ronghuanet.auth.service.IRoleService;
import cn.ronghuanet.auth.domain.Role;
import cn.ronghuanet.auth.query.RoleQuery;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    public IRoleService roleService;
    /**
     * 授权
     * Object {roleId,list}
     * 我需要一个新的对象去接受 这个参数 但是这个参数应该叫什么名字?
     * DTO data transfer Object
     *      它的作用是 前端传的数据和后台的domain差异大的情况
     *      就会去封装一个对于前端来说相近的对象
     * VO  view Object
     *     前端接受的参数 和后台传递的参数 差异较大
     */
    @PutMapping("/permission")
    public AjaxResult roleAuth(@RequestBody RoleDTO roleDTO){
        try {
            roleService.roleAuth(roleDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("授权失败！"+e.getMessage());
        }
    }
    @GetMapping("/permission/{id}")
    public AjaxResult queryRolePermissionIds(@PathVariable("id") Long id){
        try {
            List<Long> ids = roleService.queryRolePermissionIds(id);
            return AjaxResult.success(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("授权失败！"+e.getMessage());
        }
    }
    /**
     * 保存和修改公用的
     * @param role  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Role role){
        try {
            if( role.getId()!=null)
                roleService.update(role);
            else
                roleService.insert(role);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            roleService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Role role = roleService.queryById(id);
            return AjaxResult.success(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Role> list = roleService.queryAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody RoleQuery query)
    {
        try {
            PageInfo<Role> pageList = roleService.queryPage(query);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }


    /**
   * 批量删除
   */
    @PatchMapping
    public AjaxResult json(@RequestBody List<Long> ids)
    {
        try {
            roleService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量数据失败！"+e.getMessage());
        }
    }
}
